package com.example.parion;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.provider.BaseColumns;
import android.provider.CallLog;
import android.util.Log;

public class DatabaseModel {

	private final static String DB_NAME = "asd.db";
	private final static int DB_VERSION = 1;
	
	public final static String CALL_DATA = "call";
	public final static String KAKAO_DATA = "kakao";
	public final static String SMS_DATA = "sms";
	
	private static DatabaseModel instance;
	
	DatabaseHelper mDbHelper;
	
	public interface OnQueryResultListener {
		public void onQueryResult(Cursor c);
	}
	
	public static DatabaseModel getInstance() {
		if (instance == null) {
			instance = new DatabaseModel();
		}
		return instance;
	}
	
	private DatabaseModel() {
		Context context = MyApplication.getContext();
		mDbHelper = new DatabaseHelper(context, DB_NAME, null, DB_VERSION);
	}
	
	public DatabaseHelper getDatabaseHelper(){

		return mDbHelper;
	}
	
	public void saveData222(MyData data) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(DataFields.People.NAME, data.name);
		values.put(DataFields.People.PHONE, data.phone);
		values.put(DataFields.People.KAKAO_NAME, data.kakao);
		values.put(DataFields.People.NATEON_NAME, data.nateon);
		
		db.insert(DataFields.People.TABLE_NAME, null, values);
		
		
		
		db.close();
	}
	
	public void saveSMS(SMSData data){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(DataFields.SMS.PHONE, data.number);
		values.put(DataFields.SMS.NAME, data.name);
		values.put(DataFields.SMS.TIME, data.time);
		
		db.insert(DataFields.SMS.TABLE_NAME, null, values);
		
		db.close();		
	}
	
	public void saveKAKAO(KakaoData data){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(DataFields.KAKAO.NAME, data.name);
		values.put(DataFields.KAKAO.TIME, data.time);
		
		db.insert(DataFields.KAKAO.TABLE_NAME, null, values);
		
		db.close();
		
	}
	
	public void saveData(SaveData data){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(DataFields.SAVEDATA.TYPE, data.type);
		values.put(DataFields.SAVEDATA.NAME, data.name);
		values.put(DataFields.SAVEDATA.NUMBER, data.number);
		values.put(DataFields.SAVEDATA.TIME, data.time);
		values.put(DataFields.SAVEDATA.DURATION, data.call_duration);
		values.put(DataFields.SAVEDATA.CALLTYPE, data.call_type);
		
		db.insert(DataFields.SAVEDATA.TABLE_NAME, null, values);
		
		db.close();
		
	}
	
	
	public void saveCall(Cursor cur){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		
//		ContentValues values = new ContentValues();
//		values.put(DataFields.Call.PHONE, data.number);
//		values.put(DataFields.Call.NAME, data.name);
//		values.put(DataFields.Call.TIME, data.time);
//		values.put(DataFields.Call.DURATION, data.duration);
//		values.put(DataFields.Call.TYPE, data.type);
//		
//		db.insert(DataFields.Call.TABLE_NAME, null, values);
		
		if(cur.moveToFirst() && cur.getCount() > 0) {
		    while(cur.isAfterLast() == false) {
		    	
		    	SaveData data = new SaveData();
		    	
		    	long time = cur.getLong(cur.getColumnIndex(CallLog.Calls.DATE));
		    	SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String strTime = sdfNow.format(new Date(time));
				
				data.type=DatabaseModel.CALL_DATA;
				data.number=cur.getString(cur.getColumnIndex(CallLog.Calls.NUMBER));
				data.name = cur.getString(cur.getColumnIndex(CallLog.Calls.CACHED_NAME));
				data.time = strTime;
				data.call_duration = Integer.parseInt(cur.getString(cur.getColumnIndex(CallLog.Calls.DURATION)));
				data.call_type = cur.getInt(cur.getColumnIndex(CallLog.Calls.TYPE));
				
				ContentValues values = new ContentValues();
				values.put(DataFields.SAVEDATA.TYPE, data.type);				
				values.put(DataFields.SAVEDATA.NAME, data.name);
				values.put(DataFields.SAVEDATA.NUMBER, data.number);
				values.put(DataFields.SAVEDATA.TIME, data.time);
				values.put(DataFields.SAVEDATA.DURATION, data.call_duration);
				values.put(DataFields.SAVEDATA.CALLTYPE, data.call_type);
				
				db.insert(DataFields.SAVEDATA.TABLE_NAME, null, values);

				 Log.d("call history[", data.number);
		       cur.moveToNext();
		       
		       }
		    }
		
		db.close();
	}
	

	
	public final static class DataFields {
		public final static class People {
			public final static String TABLE_NAME = "my_people";
			public final static String ID = BaseColumns._ID;
			public final static String NAME = "my_name";
			public final static String PHONE = "phone_number";
			public final static String KAKAO_NAME = "kakao_name";
			public final static String NATEON_NAME = "nateon_name";
			
		}
		
		public final static class SMS{
			public final static String TABLE_NAME = "sms_data";
			public final static String ID = BaseColumns._ID;
			public final static String PHONE = "sms_number";
			public final static String NAME = "sms_name";
			public final static String TIME = "sms_time";
					
		}
		
		public final static class Call{
			public final static String TABLE_NAME = "call_data";
			public final static String ID = BaseColumns._ID;
			public final static String PHONE = "call_number";
			public final static String NAME = "call_name";
			public final static String TIME = "call_time";
			public final static String DURATION = "call_duration";
			public final static String TYPE = "call_type";
					
		}
		
		public final static class SAVEDATA{
			public final static String TABLE_NAME = "save_data";
			public final static String ID = BaseColumns._ID;
			public final static String TYPE = "type";
			public final static String NUMBER = "number";
			public final static String NAME = "name";
			public final static String TIME = "time";
			public final static String DURATION = "call_duration";
			public final static String CALLTYPE = "call_type";
					
		}
		
		public final static class KAKAO{
			public final static String TABLE_NAME = "kakao_data";
			public final static String ID = BaseColumns._ID;
			public final static String NAME = "kakao_number";
			public final static String TIME = "kakao_time";
					
		}
		
		
		
		
		
		
	}
	
	class DatabaseHelper extends SQLiteOpenHelper {


		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
//			String createTable = "CREATE TABLE "+DataFields.People.TABLE_NAME+" (" +
//					DataFields.People.ID + " integer PRIMARY KEY autoincrement," +
//					DataFields.People.NAME + " text," +
//					DataFields.People.PHONE + " text," +
//					DataFields.People.KAKAO_NAME + " text," +
//					DataFields.People.NATEON_NAME + " text);";		
//				db.execSQL(createTable);
//				
//			
//			String createTable2 = "CREATE TABLE "+DataFields.SMS.TABLE_NAME+" (" +
//					DataFields.SMS.ID + " integer PRIMARY KEY autoincrement," +
//					DataFields.SMS.PHONE + " text," +
//					DataFields.SMS.NAME + " text," +
//					DataFields.SMS.TIME + " date);";
//				db.execSQL(createTable2);
//				
//			String createTable3 = "CREATE TABLE "+DataFields.Call.TABLE_NAME+" (" +
//					DataFields.Call.ID + " integer PRIMARY KEY autoincrement," +
//					DataFields.Call.PHONE + " text," +
//					DataFields.Call.NAME + " text," +
//					DataFields.Call.TIME + " date," +
//					DataFields.Call.DURATION + " text," +
//					DataFields.Call.TYPE + " integer);";		
//				db.execSQL(createTable3);
//					
//			String createTable4 = "CREATE TABLE "+DataFields.KAKAO.TABLE_NAME+" (" +
//					DataFields.KAKAO.ID + " integer PRIMARY KEY autoincrement," +
//					DataFields.KAKAO.NAME + " text," +
//					DataFields.KAKAO.TIME + " date);";
//				db.execSQL(createTable4);
			
			String createTable = "CREATE TABLE "+DataFields.SAVEDATA.TABLE_NAME+" (" +
					DataFields.SAVEDATA.ID + " integer PRIMARY KEY autoincrement," +
					DataFields.SAVEDATA.TYPE + " text," +
					DataFields.SAVEDATA.NAME + " text," +
					DataFields.SAVEDATA.NUMBER + " text," +
					DataFields.SAVEDATA.TIME + " date," +
					DataFields.SAVEDATA.DURATION + " integer," +
					DataFields.SAVEDATA.CALLTYPE + " integer);";		
				db.execSQL(createTable);
				
		}
		

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			onCreate(db);
		}
		
		
	}
	
}
